package com.hmy.finance.controller.receipt;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.JsonResult;
import com.hmy.ccp.error.annotations.HmyGateWayController;
import com.hmy.finance.api.bo.core.request.balanceAccount.CustomerBalanceAccountInfoRequest;
import com.hmy.finance.api.bo.core.request.balanceAccount.CustomerContactorBalanceAccountInfoRequest;
import com.hmy.finance.api.bo.core.response.customerBalance.CustomerBalanceAccountInfoResponse;
import com.hmy.finance.api.bo.core.response.customerContactorBalance.CustomerContactorBalanceAccountInfoResponse;
import com.hmy.finance.bo.request.receipt.InitializeCustomerBalanceAccountRequest;
import com.hmy.finance.bo.request.receipt.InitializeCustomerContactorBalanceAccountRequest;
import com.hmy.finance.bo.response.receipt.InitializeBalanceAccountResponse;
import com.hmy.finance.service.receipt.CustomerBalanceAccountService;
import com.hmy.finance.service.receipt.CustomerContactorBalanceAccountService;
import com.hmy.finance.service.receipt.dto.CustomerBalanceAccountInfoDto;
import com.hmy.finance.service.receipt.dto.CustomerContactorBalanceAccountInfoDto;
import com.hmy.finance.service.receipt.dto.contactorAccount.ContactorInitialAccountDto;
import com.hmy.finance.service.receipt.rdto.contactorAccount.ContactorInitialAccountRdto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "收款核销")
@RestController
@RequestMapping("/api/balanceAccount")
@Validated
@Slf4j
@HmyGateWayController
public class BalanceAccountController {

    @Resource
    private CustomerContactorBalanceAccountService customerContactorBalanceAccountService;

    @Resource
    private CustomerBalanceAccountService customerBalanceAccountService;


    @ApiOperation("查询客户账户信息")
    @PostMapping("/queryCustomerBalanceAccountInfo")
    public JsonResult<CustomerBalanceAccountInfoResponse> queryCustomerBalanceAccountInfo(@RequestBody CustomerBalanceAccountInfoRequest customerBalanceAccountInfoRequest) {
        CustomerBalanceAccountInfoResponse response = customerBalanceAccountService.queryCustomerBalanceAccountInfo(BeanUtil.copyProperties(customerBalanceAccountInfoRequest, CustomerBalanceAccountInfoDto.class));
        return JsonResult.success(response);
    }

    @ApiOperation("查询客户联系人账户信息")
    @PostMapping("/queryCustomerContactorBalanceAccountInfo")
    public JsonResult<CustomerContactorBalanceAccountInfoResponse> queryCustomerContactorBalanceAccountInfo(@RequestBody CustomerContactorBalanceAccountInfoRequest customerContactorBalanceAccountInfoRequest) {
        CustomerContactorBalanceAccountInfoResponse response = customerContactorBalanceAccountService.queryCustomerContactorBalanceAccountInfo(BeanUtil.copyProperties(customerContactorBalanceAccountInfoRequest, CustomerContactorBalanceAccountInfoDto.class));
        return JsonResult.success(response);
    }

    @ApiOperation("初始化客户账户")
    @PostMapping("/initializeCustomerBalanceAccount")
    public JsonResult<InitializeBalanceAccountResponse> initializeCustomerBalanceAccount(@RequestBody InitializeCustomerBalanceAccountRequest request) {
        InitializeBalanceAccountResponse response = customerBalanceAccountService.initializeCustomerBalanceAccount(request);
        return JsonResult.success(response);
    }

    @ApiOperation("初始化客户联系人账户")
    @PostMapping("/initializeCustomerContactorBalanceAccount")
    public JsonResult<InitializeBalanceAccountResponse> initializeCustomerContactorBalanceAccount(@RequestBody InitializeCustomerContactorBalanceAccountRequest request) {
        log.info("initializeCustomerContactorBalanceAccount request:{}", JSONUtil.toJsonStr(request));
        ContactorInitialAccountDto contactorInitialAccountDto = new ContactorInitialAccountDto();
        contactorInitialAccountDto.setCustomerId(request.getCustomerId());
        contactorInitialAccountDto.setCustomerName(request.getCustomerName());
        contactorInitialAccountDto.setContactorId(request.getContactorId());
        contactorInitialAccountDto.setContactorName(request.getContactorName());
        contactorInitialAccountDto.setAmount(request.getInitBalance());
        ContactorInitialAccountRdto contactorInitialAccountRdto = customerContactorBalanceAccountService.initialCustomerContactorBalanceAccount(contactorInitialAccountDto);
        InitializeBalanceAccountResponse response = new InitializeBalanceAccountResponse();
        response.setBalanceAccountId(contactorInitialAccountRdto.getContactorId());
        response.setSuccess(contactorInitialAccountRdto.isSuccess());
        return JsonResult.success(response);
    }
}
